<?php
namespace app\beautiful\controller\admin\mall\store;
use app\BaseController;
use app\common\logic\DataLogic;
use app\beautiful\model\mall\GoodsModel;
use app\beautiful\model\mall\StoreGoodsModel;

class Goods extends BaseController{
    protected $name='囤货商品';

    public function index()
    {
        $name=$this->request->param('name');
        $bg_take_count=$this->request->param('bg_take_count/d');
        $end_take_count=$this->request->param('end_take_count/d');
        $status=$this->request->param('status/d',1);
        $where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
        ];
        $goods_where=[
            ['shop_id','=',$this->request->shopId],
            ['is_delete','=',0],
        ];
        if(!empty($name)) $goods_where[]=['name','=',$name];
        $goods_ids=GoodsModel::where($goods_where)->column('goods_id') ?? [];
        $where[]=['goods_id','in',$goods_ids];

        if(!empty($bg_take_count)){
            if(!empty($end_take_count)){
                $where[]=['take_count','between',[$bg_take_count,$end_take_count]];
            }else{
                $where[]=['take_count','>=',$bg_take_count];
            }
        }else{
            if(!empty($end_take_count)){
                $where[]=['take_count','<=',$end_take_count];
            }
        }

        if(!empty($status)) $where[]=['status','=',$status];

        $store_goods_model= new StoreGoodsModel();
        $store_goods_model = $store_goods_model->with(['goods']);
        $order=['update_time'=>'desc','store_goods_id'=>'desc'];
        $field="*";
        $append=['add_time_format'];
        $return = DataLogic::getDataList($store_goods_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);
        return $this->success($return);
    }

    public function show()
    {
        $store_goods_id=$this->request->param('store_goods_id/d',0);
        if($store_goods_id==0) return $this->success([]);

        if(null === $store_goods= StoreGoodsModel::find($store_goods_id)) return $this->error($this->name.'不存在');
        if($store_goods->is_delete!=0) return $this->error($this->name.'不存在');
        if($store_goods->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');

        $detail=[
            'goods'=>$store_goods->goods,
            'num'=>$store_goods->num,
            'total_price'=>$store_goods->total_price,
            'worth'=>$store_goods->worth,
            'fictitious_take_count'=>$store_goods->fictitious_take_count,
            'is_tui'=>$store_goods->is_tui,
            'gift_giving'=>$store_goods->gift_giving,
            'store_info'=>$store_goods->store_info,
        ];
        return $this->success(['detail'=>$detail]);
    }

    public function save()
    {
        $store_goods_id=$this->request->param('store_goods_id/d',0);
        $goods_id = $this->request->param('goods_id/d',0);
        $num=$this->request->param('num/d',0);
        $total_price=$this->request->param('total_price/f',0);
        $worth=$this->request->param('worth/f',0);
        $fictitious_take_count=$this->request->param('fictitious_take_count/d',0);
        $gift_giving=$this->request->param('gift_giving','','security_json_or_array');
        $is_tui=$this->request->param('is_tui/d',0);
        $store_info=$this->request->param('store_info');

        //判断传来的参数
        if(null === $goods= GoodsModel::find($goods_id)) return $this->error('商品不存在');
        if($goods->is_delete!=0) return $this->error('商品不存在');
        if($goods->shop_id != $this->request->shopId) return $this->error('商品不存在');
        if(empty($num) || $num<=0) return $this->error('请输入商品数量，且不能小于0');
        if(empty($total_price) || $total_price<=0) return $this->error('请输入囤货总价，且不能小于0');
        if(empty($worth) || $worth<=0) return $this->error('请输入囤货价值，且不能小于0');
        if(empty($fictitious_take_count) || $fictitious_take_count <=0) return $this->error('请输入需要展示的销量');
        if(empty($gift_giving) || !is_array($gift_giving)) return $this->error('请填写赠送的礼品');
        foreach ($gift_giving as $k => $v){
            if(empty($v['name'])) return $this->error('请输入礼品第'.($k+1).'行的名称');
            if(empty($v['price']) || $v['price'] <= 0) return $this->error('请输入规格第'.($k+1).'行的价值，且不能小于0');
            if(empty($v['num']) || $v['num'] <= 0) return $this->error('请输入规格第'.($k+1).'行的数量');
        }
        if(!in_array($is_tui,[0,1])) return $this->error('未设置是否推荐');
        if(empty($store_info)) return $this->error('请输入囤货说明');

        //操作
        if($store_goods_id != 0){
            if(null === $store_goods= StoreGoodsModel::find($store_goods_id)) return $this->error($this->name.'不存在');
            if($store_goods->is_delete!=0) return $this->error($this->name.'不存在');
            if($store_goods->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');
            if($store_goods->goods_id != $goods_id){
                if(null !== StoreGoodsModel::where([
                        ['shop_id','=',$this->request->shopId],
                        ['goods_id','=',$goods_id],
                        ['status','=',1],
                        ['is_delete','=',0],
                    ])->find()){
                    return $this->error('这个商品已经参与囤货中');
                }
            }
        }else{
            $store_goods = new StoreGoodsModel();
            $store_goods->shop_id = $this->request->shopId;
            if(null !== StoreGoodsModel::where([
                    ['shop_id','=',$this->request->shopId],
                    ['goods_id','=',$goods_id],
                    ['status','=',1],
                    ['is_delete','=',0],
                ])->find()){
                return $this->error('这个商品已经参与囤货中');
            }
        }

        $store_goods->goods_id =$goods_id;
        $store_goods->num =$num;
        $store_goods->total_price =$total_price;
        $store_goods->worth =$worth;
        $store_goods->fictitious_take_count =$fictitious_take_count;
        $store_goods->is_tui =$is_tui;
        $store_goods->gift_giving =$gift_giving;
        $store_goods->store_info =$store_info;
        $store_goods->status=1;
        $store_goods->update_time=$this->request->time();
        $store_goods->save();

        return $this->success('保存成功');
    }


    public function undercarriage()
    {
        $store_goods_id=$this->request->param('store_goods_id/d',0);

        if(null === $store_goods= StoreGoodsModel::find($store_goods_id)) return $this->error($this->name.'不存在');
        if($store_goods->is_delete!=0) return $this->error($this->name.'不存在');
        if($store_goods->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');
        if($store_goods->status == -1) return $this->error($this->name.'已下架');
        //其他判断后面补充

        $store_goods->status=-1;
        $store_goods->save();
        return $this->success('下架成功');
    }

    public function grounding()
    {
        $store_goods_id=$this->request->param('store_goods_id/d',0);

        if(null === $store_goods= StoreGoodsModel::find($store_goods_id)) return $this->error($this->name.'不存在');
        if($store_goods->is_delete!=0) return $this->error($this->name.'不存在');
        if($store_goods->shop_id != $this->request->shopId) return $this->error($this->name.'不存在');
        if($store_goods->status == 1) return $this->error($this->name.'已上架');
        //其他判断后面补充

        $store_goods->status=1;
        $store_goods->update_time=$this->request->time();
        $store_goods->save();
        return $this->success('上架成功');
    }
}
